package com.example.wechat_b.adapter;

import androidx.recyclerview.widget.RecyclerView;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.ImageView;
import android.widget.TextView;


import com.example.wechat_b.R;
import com.example.wechat_b.bean.FindRecycleBean;
import com.example.wechat_b.utils.RecycleViewItemListener;

import java.util.List;

/**
 * @author liaoqg
 * @brief description
 * @date 2021-11-05
 */
public class FindRecycleViewAdapter extends RecyclerView.Adapter<FindRecycleViewAdapter.ViewHolder> {

    private List<FindRecycleBean> beans;
    private RecycleViewItemListener listener;

    public void setListener(RecycleViewItemListener listener) {
        this.listener = listener;
    }

    static class ViewHolder extends RecyclerView.ViewHolder implements View.OnClickListener{

        ImageView leftImg,rightImg;
        TextView name,tip;
        RecycleViewItemListener listener;

        public ViewHolder(View itemView,RecycleViewItemListener listener) {
            super(itemView);
            this.listener = listener;
            leftImg = itemView.findViewById(R.id.find_round);
            rightImg = itemView.findViewById(R.id.find_rightImg);
            name = itemView.findViewById(R.id.find_name);
            tip = itemView.findViewById(R.id.find_tip);
            itemView.setOnClickListener(this);
        }

        @Override
        public void onClick(View v) {
            if (listener!=null){
                listener.onItemClick(v,getAdapterPosition());
            }
        }
    }

//    public FindRecycleViewAdapter(List<FindRecycleBean> beans){
//        this.beans = beans;
//    }
    public FindRecycleViewAdapter(List<FindRecycleBean> beans,RecycleViewItemListener listener){
        this.beans = beans;
        this.listener = listener;
    }

    @Override
    public ViewHolder onCreateViewHolder(ViewGroup parent, int viewType) {
        View view = LayoutInflater.from(parent.getContext()).inflate(R.layout.fragment_find_recycleview,parent,false);
        ViewHolder holder = new ViewHolder(view,listener);
        return holder;
    }

    @Override
    public void onBindViewHolder(FindRecycleViewAdapter.ViewHolder holder, int position) {
        FindRecycleBean bean = beans.get(position);
        holder.leftImg.setImageResource(bean.getLeftImg());
        holder.name.setText(bean.getName());
        if (bean.getRightImg()!=0){
            holder.rightImg.setImageResource(bean.getRightImg());
        }
        if (bean.getTip()!=null){
            holder.tip.setText(bean.getTip());
        }
    }


    @Override
    public int getItemCount() {
        return beans.size();
    }
}
